package me.firstapp.api.bbs;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.bbs.Reply;
import me.firstapp.service.bbs.ReplyService;

@Controller
@RequestMapping(value = "/reply")
@Api(value = "/reply", description = "回复相关接口")
public class ReplyApi extends BaseApi {

	@Autowired
	private ReplyService replyService;

	@RequestMapping(value = "findRepliesPageByParams.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询话题回复", notes = "分页查询话题回复")
	public void findRepliesPageByParams(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, Long topicId, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Reply> result = new PageObject<Reply>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<Reply> replyPage = replyService.findRepliesPageByParams(topicId, pageNo, pageSize, searchParams,
					sortParam, sortType, apiClientId);
			JsonPage<Reply> jsonPage = new JsonPage<Reply>();
			jsonPage.setList(replyPage.getEntities());
			jsonPage.setPageNo(replyPage.getPageNo());
			jsonPage.setPageSize(replyPage.getPageSize());
			jsonPage.setTotalCount(replyPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findTopicReplyCounts.do", method = RequestMethod.GET)
	@ApiOperation(value = "获取帖子回复数量", notes = "获取帖子回复数量")
	public void findTopicReplyCounts(Long topicId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> resultJsonObject = new SingleObject<Object>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Long counts = replyService.findTopicReplyCounts(topicId, apiClientId);
			resultJsonObject.setObject(counts);
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}

	@RequestMapping(value = "addReply.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增回复", notes = "新增回复")
	public void addReply(@RequestBody Map<String, Object> body, Long topicId, Long fromMemberId, Long toMemberId,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> resultJsonObject = new SingleObject<Object>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			String content = (String) body.get("content");
			Reply reply = replyService.addReply(content, topicId, fromMemberId, toMemberId, apiClientId);
			resultJsonObject.setObject(reply);
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			resultJsonObject.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonObject.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonObject);
	}

	@RequestMapping(value = "findReplyPage.do", method = RequestMethod.GET)
	@ApiOperation(value = "回复分页查询", notes = "回复分页查询")
	public void findReplyPage(@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, Long topicId,
			Long fromMemberId, Long toMemberId, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Reply> resultJsonPage = new PageObject<Reply>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<Reply> replyPage = replyService.findRepliesPageByTopicId(pageNo, pageSize, topicId, fromMemberId,
					toMemberId, apiClientId);
			JsonPage<Reply> jsonPage = new JsonPage<Reply>();
			jsonPage.setList(replyPage.getEntities());
			jsonPage.setPageNo(replyPage.getPageNo());
			jsonPage.setPageSize(replyPage.getPageSize());
			jsonPage.setTotalCount(replyPage.getEntityCount());

			resultJsonPage.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			resultJsonPage.setPage(jsonPage);
		} catch (ServiceException e) {
			resultJsonPage.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			resultJsonPage.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, resultJsonPage);
	}

	@RequestMapping(value = "/deleteReplies.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除回复", notes = "删除回复")
	public void deleteReplies(String replyIds, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			replyService.deleteReplies(replyIds, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findReplyById.do", method = RequestMethod.GET)
	@ApiOperation(value = "通过id获取回复", notes = "通过id获取回复")
	public void findReplyById(Long replyId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Reply> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Reply reply = replyService.findReplyById(replyId, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(reply);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
